#include "IniParser.h"

IniParser& IniParser::GetInstance() {
    static IniParser instance;
    return instance;
}

bool IniParser::LoadFromFile(const std::string& filePath) {
    SI_Error rc = m_ini.LoadFile(filePath.c_str());
    if (rc >= 0) {
        InitCache();
        return true;
    }
    return false;
}

bool IniParser::SaveToFile(const std::string& filePath) {
    return m_ini.SaveFile(filePath.c_str()) >= 0;
}

bool IniParser::SaveToString(std::string& data) {
    return m_ini.Save(data) >= 0;
}

void IniParser::InitCache() {
    m_sections.clear();
    m_sectionKeys.clear();
    m_valueCache.clear();

    CSimpleIniA::TNamesDepend sections;
    m_ini.GetAllSections(sections);
    
    // 使用传统迭代器替代基于范围的for循环
    CSimpleIniA::TNamesDepend::const_iterator sectIt;
    for (sectIt = sections.begin(); sectIt != sections.end(); ++sectIt) {
        const std::string sectionName(sectIt->pItem);
        m_sections.push_back(sectionName);

        CSimpleIniA::TNamesDepend keys;
        m_ini.GetAllKeys(sectionName.c_str(), keys);
        
        std::vector<std::string>& sectionKeys = m_sectionKeys[sectionName];
        CSimpleIniA::TNamesDepend::const_iterator keyIt;
        for (keyIt = keys.begin(); keyIt != keys.end(); ++keyIt) {
            const std::string keyName(keyIt->pItem);
            const char* value = m_ini.GetValue(sectionName.c_str(), keyName.c_str(), "");
            
            sectionKeys.push_back(keyName);
            m_valueCache[std::make_pair(sectionName, keyName)] = value ? value : "";
        }
    }
}

const std::vector<std::string>& IniParser::GetAllSections() const {
    return m_sections;
}

const std::vector<std::string> IniParser::GetSectionKeys(const std::string& section) const {
    // 替换auto为具体类型
    std::map<std::string, std::vector<std::string> >::const_iterator it = m_sectionKeys.find(section);
    return it != m_sectionKeys.end() ? it->second : std::vector<std::string>();
}

std::string IniParser::GetValue(const std::string& section, const std::string& key, const std::string& defaultValue) const {
    // 替换auto为具体类型
    std::map<std::pair<std::string, std::string>, std::string>::const_iterator it = 
        m_valueCache.find(std::make_pair(section, key));
    return it != m_valueCache.end() ? it->second : defaultValue;
}

bool IniParser::SetValue(const std::string& section, const std::string& key, const std::string& value) {
    SI_Error rc = m_ini.SetValue(section.c_str(), key.c_str(), value.c_str());
    if (rc >= 0) {
        InitCache();  // 更新缓存
        return true;
    }
    return false;
}

bool IniParser::DeleteKey(const std::string& section, const std::string& key, bool removeSectionIfEmpty) {
    bool result = m_ini.Delete(section.c_str(), key.c_str(), removeSectionIfEmpty);
    if (result) {
        InitCache();  // 更新缓存
    }
    return result;
}

bool IniParser::DeleteSection(const std::string& section) {
    bool result = m_ini.Delete(section.c_str(), nullptr);
    if (result) {
        InitCache();  // 更新缓存
    }
    return result;
}